package com.rchat.pocmini.activity.binddevice;

import android.text.TextUtils;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.account.PocAccount;
import com.rchat.pocmini.bean.UnBindBluetoothBean;
import com.rchat.pocmini.bean2eventbus.BluetoothBean;
import com.rchat.pocmini.bluetooth.BTService;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;
import com.rchat.pocmini.util.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by itsdf07 on 2017/9/12.
 */

public class BindDevicePresenterImpl extends BindDeviceContract.Presenter {
    /**
     * 扫激活码的requestCode
     */
    public static final int REQUEST_CODE_ACTIVATE = 1;
    /**
     * 扫设备地址码绑定的requestCode
     */
    public static final int REQUEST_CODE_BIND = 2;

    /**
     * 扫设备地址码解绑的requestCode
     */
    public static final int REQUEST_CODE_UNBIND = 3;

    /**
     * 扫设备地址码配对蓝牙设备的requestCode
     */
    public static final int REQUEST_CODE_AUTOPAIR = 4;

    private ArrayList<String> mBindDevices = new ArrayList<>();

    @Override
    public void onPStart() {
        super.onPStart();
        initDevicesDate();
    }

    /**
     * 初始化/更新账号绑定的蓝牙设备列表
     */
    private void initDevicesDate() {
        if (mBindDevices == null) {
            mBindDevices = new ArrayList<>();
        } else {
            mBindDevices.clear();
        }
        String bindDeviceString = (String) SPUtil.get(mActivity, SPUtil.SPKeyGlobal.KEY_BIND_BLUETOOTH_MAC, "");
        String[] bindedDevices = bindDeviceString.split(",");
        for (String deviceMac : bindedDevices) {
            if (TextUtils.isEmpty(deviceMac)) {
                continue;
            }
            mBindDevices.add(deviceMac);
        }
        mView.updateList(mBindDevices);
        ALog.d("mBindDevices = %s", mBindDevices.size());
    }

    @Override
    void activate(String qrCode) {
        JSONObject root = new JSONObject();
        try {
            root.put("code", HttpCodeGlobal.REQ_CODE_1043);
            JSONObject data = new JSONObject();
            data.put("qrCode", qrCode);
            data.put("userName", PocAccount.getUserId());
            root.put("data", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.activate(UrlsGlobal.getUrl(UrlsGlobal.URL_DEVICE_BLUETOOTH), root.toString(), new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
                ALog.d("desc = %s", bean.getDesc());
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
                ALog.d("desc = %s", bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (mView == null) {
                    return;
                }
                mView.dismissLoading();
            }
        });

    }

    @Override
    void bindDevice(final String macAddress) {
        JSONObject root = new JSONObject();
        try {
            root.put("code", HttpCodeGlobal.REQ_CODE_1042);
            JSONObject data = new JSONObject();
            data.put("mac", macAddress);
            data.put("userName", PocAccount.getUserId());
            root.put("data", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.bindDevice(UrlsGlobal.getUrl(UrlsGlobal.URL_DEVICE_BLUETOOTH), root.toString(), new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                String bindDeviceString = (String) SPUtil.get(mActivity, SPUtil.SPKeyGlobal.KEY_BIND_BLUETOOTH_MAC, "");
                bindDeviceString += macAddress + ",";
                SPUtil.put(mActivity, SPUtil.SPKeyGlobal.KEY_BIND_BLUETOOTH_MAC, bindDeviceString);
                initDevicesDate();

                BluetoothBean bluetoothBean = new BluetoothBean();
                bluetoothBean.setCurrentEvent(BluetoothBean.EVENT_BIND);
                bluetoothBean.setTarget(this.getClass(), BTService.class, "onBluetoothEventBus");
                bluetoothBean.setAddress(macAddress);
                bluetoothBean.setBind(true);
                EventBus.getDefault().post(bluetoothBean);
                ALog.d("desc = %s", bean.getDesc());
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
                ALog.d("desc = %s", bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (mView == null) {
                    return;
                }
                mView.dismissLoading();
            }
        });
    }

    @Override
    void unBindDevice(final String macAddress) {
        JSONObject root = new JSONObject();
        try {
            root.put("code", HttpCodeGlobal.REQ_CODE_1044);
            JSONObject data = new JSONObject();
            data.put("mac", macAddress);
            data.put("userName", PocAccount.getUserId());
            root.put("data", data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.unBindDevice(UrlsGlobal.getUrl(UrlsGlobal.URL_DEVICE_BLUETOOTH), root.toString(), new HttpCallbackImpl<UnBindBluetoothBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(UnBindBluetoothBean bean) {
                if (mView == null) {
                    return;
                }
                String bindDeviceString = (String) SPUtil.get(mActivity, SPUtil.SPKeyGlobal.KEY_BIND_BLUETOOTH_MAC, "");
                String[] bindedDevices = bindDeviceString.split(macAddress + ",");
                String macs = "";
                for (String mac : bindedDevices) {
                    macs += mac;
                }
                SPUtil.put(mActivity, SPUtil.SPKeyGlobal.KEY_BIND_BLUETOOTH_MAC, macs);
                initDevicesDate();

                BluetoothBean bluetoothBean = new BluetoothBean();
                bluetoothBean.setCurrentEvent(BluetoothBean.EVENT_UNBIND);
                bluetoothBean.setTarget(this.getClass(), BTService.class, "onBluetoothEventBus");
                bluetoothBean.setAddress(macAddress);
                bluetoothBean.setBind(false);
                EventBus.getDefault().post(bluetoothBean);
                ALog.d("macs = %s", macs);
                ALog.d("desc = %s", bean.getDesc());

                //展示解绑的设置的mac地址二维码
                mView.showUnbindMacAddress(bean.getData().getBluetoothMac());
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }

                mView.showShortToast(bean.getDesc());
                ALog.d("desc = %s", bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (mView == null) {
                    return;
                }
                mView.dismissLoading();
            }
        });
    }

}
